﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using App1AppHandler;

namespace Reborn_Server.App1AppHandler
{
    public class Spell_SummonSkeleton : TargetSpell
    {
        public void Cast(CreatureBase myCreatureBase, int CastLevel, long CreatureGUID)
        {
            List<MapItem> MapItemsWithin12;
            if (base.InitializeCast(myCreatureBase, CastLevel, CreatureGUID, false, out MapItemsWithin12) == false)
                return;

            if (myCreatureBase is Character)
            {
                Character character = (Character)myCreatureBase;
                if (character.petManager.NumPets() > 3)
                {
                    Message message = new Message();
                    message.Type = "server";
                    message.CreatureID = character.CreatureID;
                    message.Text = "You have to many pets";

                    character.PlaySound("Ding", character.MapX, character.MapY);
                    character.AddMessage(message);

                    return;
                }
                List<Pet_Controller> pets = character.petManager.GetPets();
                foreach (Pet_Controller pet in pets)
                {
                    if (pet.MyCreature.ImageURL == "Skeleton2")
                    {
                        Message message = new Message();
                        message.Type = "server";
                        message.CreatureID = character.CreatureID;
                        message.Text = "You can not have more than 1 Skeleton";

                        character.PlaySound("Ding", character.MapX, character.MapY);
                        character.AddMessage(message);

                        return;
                    }
                }
            }

            MapItem currentMapItem = null;
            foreach (MapItem mapItem in MapItemsWithin12)
            {
                if (mapItem.MapX == myCreatureBase.MapX && mapItem.MapY == myCreatureBase.MapY)
                {
                    currentMapItem = mapItem;
                    break;
                }
            }

            Creature skeleton = new Creature(myCreatureBase.GameHandler);
            CreatureBase ig = (CreatureBase)skeleton;
            skeleton.petController = new Pet_Controller(ig, myCreatureBase, currentMapItem);
            skeleton.petController.autoTauntAmount = SkillManager.AdjustForCombat(CastLevel, CastingCreatureInt / 2, CastingCreatureInt / 2);

            //skeleton.IceEater = true;
            //skeleton.MaxHPBase = myCreatureBase.MaxHP + (myCreatureBase.WisdomTotal * 2);
            skeleton.MaxHPBase = SkillManager.AdjustForCombat(CastLevel, 0, CastingCreatureWis / 2) * 9;
            skeleton.HP = skeleton.MaxHPBase;

            skeleton.MaxStaminaBase = 1000;
            skeleton.Stamina = 1000;

            skeleton.MaxManaBase = 0;
            skeleton.Mana = 0;

            skeleton.Agility = 2 * CastLevel;
            skeleton.Armor = SkillManager.AdjustForCombat(CastLevel, 0, CastingCreatureWis / 3);
            skeleton.ResistUnholy = SkillManager.AdjustForCombat(CastLevel, 100, CastingCreatureWis);
            skeleton.ResistFire = SkillManager.AdjustForCombat(CastLevel / 2, 100, CastingCreatureWis / 2);
            skeleton.ResistIce = SkillManager.AdjustForCombat(CastLevel / 2, 100, CastingCreatureWis / 2);
            skeleton.ResistMagic = SkillManager.AdjustForCombat(CastLevel / 2, 100, CastingCreatureWis);
            skeleton.ResistPhysical = SkillManager.AdjustForCombat(CastLevel / 2, 100, CastingCreatureWis / 2);

            skeleton.Dexterity = CastingCreatureInt / 2;
            skeleton.Strength = CastingCreatureInt / 4;
            skeleton.Wisdom = 10;
            skeleton.Intelligence = 10;

            skeleton.Defense = CastingCreatureWis / 2;
            skeleton.DefenseSkill = CastLevel;
            skeleton.HandBlockWeapon = "hand";

            skeleton.RushAttack = true;
            skeleton.Water = true;
            skeleton.Air = false;
            skeleton.Land = true;

            CreatureAttackWeapon skeletonWeapon = new CreatureAttackWeapon();
            skeletonWeapon.Accuracy = SkillManager.AdjustForCombat(CastLevel, 0, CastingCreatureInt / 2);
            skeletonWeapon.Description = "strikes {name}";
            skeletonWeapon.DmgHigh = SkillManager.AdjustForCombat(CastLevel / 2, 0, CastingCreatureInt / 3) + 15;
            skeletonWeapon.DmgLow = SkillManager.AdjustForCombat(CastLevel / 2, 0, CastingCreatureInt / 3) - 15;
            skeletonWeapon.Frequency = 1;
            skeletonWeapon.Sequence = 1;
            skeletonWeapon.SkillLevel = CastLevel;
            skeletonWeapon.Speed = 3.1M;

            skeleton.attackSequenceManager.Attacks.Add(skeletonWeapon);

            skeleton.Name = myCreatureBase.Name + "'s Skeleton";
            skeleton.SoundDeath = "Rattle";

            //No corpse image will cause DropLoot() to not generate a corpse
            skeleton.CorpseImageURL = "";

            skeleton.CreatureID = myCreatureBase.GameHandler.ItemIDGen.GetCreatureUID();
            skeleton.CreatureType = "Creature";
            skeleton.Faction = myCreatureBase.Faction;
            skeleton.ID = -1;
            skeleton.ImageURL = "Skeleton2";
            skeleton.Instance = myCreatureBase.Instance;
            skeleton.MapID = myCreatureBase.MapID;

            skeleton.MaxMoves = 4;

            skeleton.Timer_Regen.Start();
            skeleton.creatureAI.Timer_CreatureRound.Start();

            MapItem mapitemToUpdate = myCreatureBase.currentMapItem;
            List<Character> CharsToUpdate = new List<Character>();
            foreach (MapItem mapitem in myCreatureBase.currentMapItem.mapItemsWithin8)
            {
                foreach (CreatureBase creaturebase in mapitem.CreaturesBases)
                {
                    if (creaturebase is Character)
                        CharsToUpdate.Add((Character)creaturebase);
                }
                if (mapitem.MapX == myCreatureBase.MapX && mapitem.MapY == myCreatureBase.MapY)
                {
                    skeleton.SetPosition(mapitem);
                    mapitem.CreaturesBases.Add(skeleton);
                    mapitemToUpdate = mapitem;
                }
            }
            foreach (Character character in CharsToUpdate)
            {
                //character.PlaySound(soundToPlay.Name, soundToPlay.MapX, soundToPlay.MapY);
                character.AddMapItem(mapitemToUpdate);

                if (character == myCreatureBase)
                    character.petManager.AddPet(skeleton.petController);
            }
        }
    }
}
